
import Core.ConnectionHandler;
import Core.IChat;
import Core.IConnectionListener;
import Core.IMessageHandler;
import Core.IMessageProvider;
import Core.MessageBuilder;
import Core.MessageType;
import Message.LogInResultMessage;
import Message.LogInResultMessageHandler;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.SocketConnection;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Mr Bao
 */
public class MobileClient implements IChat, IConnectionListener {

    private String host;
    private int port;
    private long userId;
    private static IMessageProvider messageProvider;
    protected ConnectionHandler connectionHandler;
    private boolean isConnect = false;
    private Vector queueMessage;

    static {
        Boot();
    }

    private static void Boot() {
        messageProvider = new MessageProvider();
        messageProvider.Register(LogInResultMessage.class, new LogInResultMessageHandler());
    }

    public boolean Connect() {
        try {
            if (!isConnect) {
                SocketConnection socket = (SocketConnection) Connector.open("socket://" + host + ":" + port);
                socket.setSocketOption(socket.RCVBUF, 16384);
                connectionHandler = new ConnectionHandler(socket);
                connectionHandler.SetListener(this);
                isConnect = true;
            }
        } catch (Exception e) {
        }
        return isConnect;
    }

    public MobileClient(String host, int port, Vector queueMessage) {
    }

    public void ConnectionFailed() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void Login(String username, String password, boolean invisible) {
        try {
            Connect();
            if (isConnect) {
                connectionHandler.QueueMessage(MessageBuilder.BuildLogInMessage(username, password, invisible));
            }
        } catch (Exception e) {
        }
    }

    public void SendMessage(String to, String message) {
        if (isConnect) {
            connectionHandler.QueueMessage(MessageBuilder.BuildSendTextMessage(to, message));
        }
    }

    public void SendBuzz(String to) {
        if (isConnect) {
            connectionHandler.QueueMessage(MessageBuilder.BuildSendBuzzMessage(to));
        }
    }

    public void ChangeStatus(String newStatus, boolean showBusyIcon) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void ChangeStatus(int statusType) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void Invisible(boolean invisible) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void AddFriend(String username, String groupName) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void RemoveFriend(String username, String groupName) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void RejectFriend(String friend, String username, String message) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void AcceptFriend(String friend) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void AccepConfInvite(String room, String yId) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void IncomingMessage(String message) {
        IMessageHandler handler = null;
        try {
            if (message.startsWith(MessageType.LogInResult)) {
                handler = messageProvider.GetMessageHandler(LogInResultMessage.class);
                LogInResultMessage logInResultMessage = (LogInResultMessage) handler.Process(message);
                if (logInResultMessage.userId < 0) {
                } else {
                    userId = logInResultMessage.userId;
                }
            }
            handler = null;
            message = null;
        } catch (Exception e) {
        }
    }

    public void SocketClosed() {
        isConnect = false;
        synchronized (queueMessage) {
            queueMessage.addElement("off:5");
            queueMessage.notifyAll();
        }
    }
}
